//
//  VisitorLoginView.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/8.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    func visitorWillLogin()
    func visitorWillRegister()
    
}

class VisitorLoginView: UIView {
    
   weak var visitorDelegate: VisitorLoginViewDelegate?
    
    func loginBtnDidClick (){
        
        visitorDelegate?.visitorWillLogin()
        
    }
    func registerBtnDidClick (){
        
        visitorDelegate?.visitorWillRegister()
        
    }

    //重写构造方法
    init(){
        
        super.init(frame:CGRectZero)
        
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
        
    }
    
    func setUIInfo (imageName: String? ,title: String){
        
        if imageName != nil{
            circleView.hidden = true
            backView.hidden = true
            iconView.image = UIImage(named: imageName!)
            
            tipLabel.text = title
            
        }else{
            
            backView.hidden = false
            
            startAnimation()
            
        }
        

      
    }
    
    func startAnimation (){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 9
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    func setUI(){
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置自动布局约束无效
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //view1.attr1 = view2.attr2 * multiplier + constant
        //动态图的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //小房子的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //提示文字的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //背景图的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backview]-0-|", options: [], metrics: nil, views: ["backview" : backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backview]-(-35)-[regiserBtn]", options: [], metrics: nil, views: ["backview" : backView,"regiserBtn": registerBtn]))

        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        loginBtn.addTarget(self, action:"loginBtnDidClick" , forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action:"registerBtnDidClick" , forControlEvents: .TouchUpInside)
        
        
    }
    //懒加载动态图片
    lazy var circleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //懒加载小房子图片
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //背景图的懒加载
    lazy var backView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //提示文字的懒加载
    lazy var tipLabel : UILabel = {
       
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        
        return label
    }()
    
    //登录按钮的懒加载
    lazy var loginBtn : UIButton = {
    
    var loginBtn = UIButton()
        
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
    
    return loginBtn
    }()
    //注册按钮的懒加载
    lazy var registerBtn : UIButton = {
        
        var registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return registerBtn
    }()
}
